package me.flyray.bsin.filter;

import com.alipay.sofa.rpc.core.exception.SofaRpcException;
import com.alipay.sofa.rpc.core.request.SofaRequest;
import com.alipay.sofa.rpc.core.response.SofaResponse;
import com.alipay.sofa.rpc.ext.Extension;
import com.alipay.sofa.rpc.filter.AutoActive;
import com.alipay.sofa.rpc.filter.Filter;
import com.alipay.sofa.rpc.filter.FilterInvoker;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HLW
 */
@Slf4j
@Extension(value = "sofaRpcRequestFilter",order = Integer.MAX_VALUE)
@AutoActive(providerSide = true)
public class SofaRpcRequestFilter extends Filter {


    @Override
    public SofaResponse invoke(FilterInvoker invoker, SofaRequest request) throws SofaRpcException {
        String baseLog = "AppName =[" + request.getTargetAppName() + "],interfaceName=[" + request.getInterfaceName() + "],methodName=[" + request.getMethodName() + "],version=[" + invoker.getConfig().getUniqueId() + "]";
        SofaResponse response = invoker.invoke(request);
        if (response.getAppResponse() instanceof Exception) {
            log.error("SOFA - 服务异常: {},parameter={}", baseLog, request.getMethodArgs(), response.getAppResponse());
        } else {
            log.info("SOFA - 服务调用: {},parameter={}", baseLog, request.getMethodArgs());
        }
        return response;
    }
}
